# Copyright 2014 Sean Burford <vk2ete@vk2ete.net>.  All Rights Reserved.
from google.appengine.ext import db


def Put(entries):
  return db.put(entries)


class ICalEvent(db.Model):
  # Metadata
  feed_name = db.StringProperty()
  src = db.LinkProperty()
  timezone = db.StringProperty()
  callsign = db.StringProperty()

  # Actual fields
  uid = db.StringProperty()
  subject = db.StringProperty()
  date_start = db.DateTimeProperty()
  date_end = db.DateTimeProperty()
  description = db.TextProperty()
  comment = db.StringProperty()
  private = db.BooleanProperty()
  categories = db.StringListProperty()
  location = db.StringProperty()


class LocationClue(db.Model):
  feed_name = db.StringProperty()
  callsign = db.StringProperty()
  keyword = db.StringProperty()
  location = db.StringProperty()


def CalendarEventToDS(feed_name, evt):
  ds_event = ICalEvent(key_name=evt.UID())

  ds_event.feed_name = feed_name
  ds_event.src = evt.src
  ds_event.timezone = evt.timezone
  ds_event.callsign = evt.callsign

  ds_event.uid = evt.UID()
  ds_event.subject = evt.subject
  ds_event.date_start = evt.date_start
  ds_event.date_end = evt.date_end
  ds_event.description = evt.description
  ds_event.comment = evt.comment
  ds_event.private = evt.private
  ds_event.categories = sorted(list(evt.categories))
  ds_event.location = evt.location
  return ds_event


def Find(feed_name, between_start, between_end):
  q = ICalEvent.all()
  q.filter("feed_name =", feed_name)
  q.filter("date_start >=", between_start)
  q.filter("date_start <=", between_end)
  q.order("-date_start")
  return q.run()


def GetLocationClues(feed_name):
  q = LocationClue.all()
  q.filter("feed_name =", feed_name)
  return list(q.run())

